
#event study functions----------------------------------------------------------------------
make_twfe_event_study <- function(df, govt_prop, pop_prop, occ_filter, lab1, lab2){
  var_govt_prop = enquo(govt_prop)
  var_pop_prop = enquo(pop_prop)
  
  df <-
    df %>%
    filter_(occ_filter) %>%
    mutate(
      #outcome variables
      govt_prop = !!var_govt_prop,
      pop_prop = !!var_pop_prop,
      over_prop = govt_prop - pop_prop,
      
      #independent variables    
      ever_treat = ((!is.na(treat))*1),
      distance = case_when(ever_treat == 0 ~ 0,
                           TRUE ~ as.numeric(YEAR) - as.numeric(all)),
      grouped_distance = floor(distance/10))
  
  feols(govt_prop ~ i(grouped_distance, ever_treat, ref = -1) + pop_prop | city + YEAR, cluster = ~city, data = df) %>%
    tidy() %>%
    select(term, estimate, std.error) %>%
    add_row(term = "-1",  estimate = 0, std.error = NA_real_) %>%
    mutate(val = str_remove_all(term, "[:alpha:]|\\_|\\:"),
           lab1 = lab1,
           lab2 = lab2)
}

library(Hmisc)

city_gov_pre_treat <-
  analysis_df_3 %>%
  group_by(city) %>%
  mutate(max_year = max(YEAR[treat==0])) %>%
  filter(YEAR==max_year) %>%
  select(city, size_last = white_x_foreign_born) %>%
  distinct() %>%
  ungroup() %>%
  mutate(state = str_remove(city, '.*, '),
         quintile_last = cut2(size_last, g = 5)) %>%
  mutate(quintile_last = str_remove_all(quintile_last, "\\[|\\)|\\]|\\(")) %>%
  separate(., col = quintile_last, into = c("min", "max"), sep = "\\,") %>%
  mutate(min = paste0(round(as.numeric(min)*100, 1)),
         max = paste0(round(as.numeric(max)*100, 1),"%")) %>%
  mutate(quintile = paste0("[", min, ", ", max, "]"))

factor_levels <-
  city_gov_pre_treat %>%
  select(min, quintile) %>%
  distinct() %>%
  arrange(as.numeric(min)) %>%
  pull(quintile)


size_data <-
  left_join(analysis_df_3,
            city_gov_pre_treat)



quintiles <- unique(city_gov_pre_treat$quintile)

bc_size_out1 <- make_twfe_event_study(size_data %>% filter(quintile==quintiles[1]), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = quintiles[1])
bc_size_out2 <- make_twfe_event_study(size_data %>% filter(quintile==quintiles[2]), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = quintiles[2])
bc_size_out3 <- make_twfe_event_study(size_data %>% filter(quintile==quintiles[3]), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = quintiles[3])
bc_size_out4 <- make_twfe_event_study(size_data %>% filter(quintile==quintiles[4]), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = quintiles[4])
bc_size_out5 <- make_twfe_event_study(size_data %>% filter(quintile==quintiles[5]), govt_white_x_foreign_born, white_x_foreign_born, 'occ == "blue_collar"', lab1 = "White Foreign Born", lab2 = quintiles[5])

size_plot <-
  bind_rows(bc_size_out1, bc_size_out2, bc_size_out3,
            bc_size_out4, bc_size_out5) %>%
  mutate(val = as.numeric(as.character(val))) %>%
  mutate(lab2 = factor(lab2, levels = factor_levels)) %>%
  mutate(pre_post = (val > -1)*1) %>%
  ggplot(aes(x=val, y = estimate, shape = lab2, color = lab2, group = lab2, alpha = factor(pre_post))) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymin = estimate - 1.96*std.error, ymax = estimate + 1.96*std.error), 
                position = position_dodge(width = 0.5),
                width = 0) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  #facet_wrap(lab2~.) +
  theme_bw() +
  scale_colour_grey() +
  xlab("Decades Before/After Civil Service Reform") +
  ylab("Effect of Civil Service Reform on Representation") +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        legend.position = "right",
        strip.background = element_blank(),
        #legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  scale_x_continuous(breaks = seq(-4, 4, by = 1), 
                     limits = c(-4.5, 4.5)) +
  scale_alpha_manual(values = c(0.4, 1)) +
  guides(alpha = FALSE) +
  ylim(-0.3, 0.3) +
  guides(color = guide_legend("% White Foreign-Born\n(Pre-Reform)"), 
         shape = guide_legend("% White Foreign-Born\n(Pre-Reform)")) +
  theme(text=element_text(size=9))


#ggsave('../Apps/Overleaf/merit paper/outputs/immpct_pre_bc_white.png', plot = size_plot, width=6, height=3)
ggsave('./replication_file/_5_outputs/figures/figure_a6.png', plot = size_plot, width=6, height=3)


